package coms.delegates;

import EJB.EJBLocator;
import coms.dto.Conference;
import coms.dto.ConferenceRegistration;
import coms.dto.Event;
import coms.ejb.EventBeanRemote;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.jms.*;

public class EventDelegate implements IDelegate<Event>{

    private Queue queue;
    private QueueConnectionFactory factory;
    private QueueConnection connection;
    private QueueSession session;
    private QueueSender sender;
    private String emailAddresses;
    private MapMessage msg;
    private Conference conference;
    private EventBeanRemote eventBean;
    private ConferenceRegistrationDelegate registeredConferenceDelegate;
    private ConferenceDelegate conferenceDelegate;

    
    public EventDelegate() {
        registeredConferenceDelegate = new ConferenceRegistrationDelegate();
        conferenceDelegate = new ConferenceDelegate();
        emailAddresses = "";
        try {
            this.eventBean = EJBLocator.lookupEventBean(); 
            this.queue = (Queue)EJBLocator.lookupEmailMDB();
            this.factory = (QueueConnectionFactory)EJBLocator.lookupEmailFactory();
            this.connection = (QueueConnection)factory.createQueueConnection();
            this.session = connection.createQueueSession(false, QueueSession.AUTO_ACKNOWLEDGE);
            this.sender = session.createSender(queue);  
        } catch (Exception ex) {
            Logger.getLogger(EventDelegate.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    @Override
    public boolean create(Event event) throws JMSException{
        eventBean.create(event);
        sendEmail(event);
        return true;
    }
    
    public void sendEmail(Event event) throws JMSException{
            //client creates the connection, session and message sender:
            //Queue queue = (Queue)ctx.lookup("jms/MDB30");
            List<ConferenceRegistration> participantList = getConferenceRegistration(event);
            if(participantList.isEmpty()){
                System.out.println("No Participants...");
                return;
            }
             //create and set a message to send
            msg = session.createMapMessage();

            for(int i=0;i<participantList.size();i++){
            emailAddresses += participantList.get(i).getParticipant().getEmailAddress() + ",";
            }
            
            msg.setString("recipient", emailAddresses);
            //maybe need to customise the message
            msg.setString("message", "Dear Participants, The Event has been confirmed. This is an Event notification for mass sending.");
            //finally client send messages asynchronously to the queue
            sender.send(msg);
            
            System.out.println("*******************Email Created");
            /*
             * not sure if need to close the connection
            sender.close();
            session.close();
            connection.close();
            * 
            */
    }
    
    public List<ConferenceRegistration> getConferenceRegistration(Event event){
            conference = conferenceDelegate.getById(event.getConference().getId());
            List<ConferenceRegistration> conferenceRegistration = registeredConferenceDelegate.getByConference(conference);
            return conferenceRegistration;
    }

    @Override
    public boolean delete(Event event) {
        boolean deleted = eventBean.delete(event);
        return deleted;
    }

    @Override
    public boolean update(Event event) {
        eventBean.update(event);
        return true;
    }

    @Override
    public List<Event> search(Event event) throws Exception {
       return eventBean.search(event);
    }

    @Override
    public List<Event> findAll() throws Exception {
         return eventBean.findAll();
    }
    
     @Override
    public Event getById(int id) throws Exception {
       return eventBean.getById(id);
    }
}
